package com.dhsc.service.impl.qsjk;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.dhsc.domain.jkzx.qsjk.TQsjkrwTzVo;
import com.dhsc.domain.jkzx.qsjk.TQsjkrwVo;
import com.dhsc.domain.yypt.LayuiTableVo;
import com.dhsc.domain.yypt.ResponseObject;
import com.dhsc.domain.yypt.sys.TXtdmVo;
import com.dhsc.exception.MyException;
import com.dhsc.local.dao.common.ICommonDao;
import com.dhsc.local.dao.qsjk.ITQsjkDao;
import com.dhsc.service.qsjk.ITQsjkService;
import com.dhsc.service.qsjk.ITQsjkZxService;
import com.dhsc.util.CommonUtil;
import com.github.pagehelper.PageInfo;
import com.github.pagehelper.page.PageMethod;

import lombok.extern.slf4j.Slf4j;

@Service
@Slf4j
public class TQsjkServiceImpl implements ITQsjkService{
	
	@Autowired
	private ITQsjkDao qsjkDao;
	
	@Autowired
	private ICommonDao commonDao;
	
	@Autowired
	private ITQsjkZxService qsjkZxService;
	
	@Override
	public LayuiTableVo queryTQsjkrw(TQsjkrwVo q) {
		LayuiTableVo obj = new LayuiTableVo();
        try{
            PageMethod.startPage(q.getPage(), q.getLimit());
            List<TQsjkrwVo> list = qsjkDao.queryTQsjkrw(q);
            PageInfo<TQsjkrwVo> pageInfo = new PageInfo<>(list);
            
            if(CommonUtil.isNotEmptyList(list))
            {
            	TXtdmVo x = new TXtdmVo();
            	x.setFldmstr("QSJK_MK;QSJK_LX;QSJK_YW;QSJK_CFZX;QSJK_TZFS;QSJK_ZXJG");
            	List<TXtdmVo> xtdmlist = commonDao.queryQsTXtdmByFldmStr(x);
            	for(int i = 0;i < list.size();i ++)
            	{
            		for(int j = 0;j < xtdmlist.size();j ++)
            		{
            			if("QSJK_MK".equals(xtdmlist.get(j).getFldm()) && list.get(i).getJkmk().equals(xtdmlist.get(j).getValue()))
            			{
            				list.get(i).setJkmk_name(xtdmlist.get(j).getName());
            			}
            			if("QSJK_YW".equals(xtdmlist.get(j).getFldm()) && list.get(i).getJkyw().equals(xtdmlist.get(j).getValue()))
            			{
            				list.get(i).setJkyw_name(xtdmlist.get(j).getName());
            			}
            			if("QSJK_LX".equals(xtdmlist.get(j).getFldm()) && list.get(i).getJklx().equals(xtdmlist.get(j).getValue()))
            			{
            				list.get(i).setJklx_name(xtdmlist.get(j).getName());
            			}
            			if("QSJK_CFZX".equals(xtdmlist.get(j).getFldm()) && list.get(i).getCfzx().equals(xtdmlist.get(j).getValue()))
            			{
            				list.get(i).setCfzx_name(xtdmlist.get(j).getName());
            			}
            			if("QSJK_TZFS".equals(xtdmlist.get(j).getFldm()) && list.get(i).getTzfs().equals(xtdmlist.get(j).getValue()))
            			{
            				list.get(i).setTzfs_name(xtdmlist.get(j).getName());
            			}
            			if("QSJK_ZXJG".equals(xtdmlist.get(j).getFldm()) && CommonUtil.isNotEmptyObj(list.get(i).getLast_zxjg()) && list.get(i).getLast_zxjg().equals(xtdmlist.get(j).getValue()))
            			{
            				list.get(i).setLast_zxjg_name(xtdmlist.get(j).getName());
            			}
            		}
            	}
            }
            
            obj.setData(list);
            obj.setMsg("");
            obj.setCount(pageInfo.getTotal());
            obj.setCode(0);
        }catch(Exception e){
            log.error("查询清算监控任务出错", e);
            throw new MyException("查询清算监控任务出错",e);
        }
        return obj;
	}

	@Override
	public ResponseObject saveTQsjkrw(TQsjkrwVo q) {
		ResponseObject obj = new ResponseObject();
		try {
			//实体类中null转""
			TQsjkrwVo qsjk = CommonUtil.noNullStringAttr(q);
			
			if(qsjk.getId() == 0){
				qsjk.setZt(1);
				qsjk.setQybz(1);
				qsjk.setLast_zxjg("1");
				qsjkDao.insertTQsjkrw(qsjk);
			}else{
				qsjkDao.updateTQsjkrw(qsjk);
			}
			obj.setStatus(200);
		} catch (Exception e) {
			log.error("保存清算监控任务失败",e);
			throw new MyException("保存清算监控任务失败",e);
		}
		return obj;
	}
	
	@Override
	public ResponseObject updateTQsjkrwQybz(TQsjkrwVo q) {
		ResponseObject obj = new ResponseObject();
		try {
			qsjkDao.updateTQsjkrwQybz(q);
			obj.setStatus(200);
		} catch (Exception e) {
			log.error("更新清算监控任务启用标志失败",e);
			throw new MyException("更新清算监控任务启用标志失败",e);
		}
		return obj;
	}

	@Override
	public ResponseObject delTQsjkrw(TQsjkrwVo q) {
		ResponseObject obj = new ResponseObject();
		try {
			qsjkDao.delTQsjkrw(q);
			obj.setStatus(200);
		} catch (Exception e) {
			log.error("删除清算监控任务失败",e);
			throw new MyException("删除清算监控任务失败",e);
		}
		return obj;
	}

	@Override
	public ResponseObject queryTQsjkrwHave(TQsjkrwVo q) {
		ResponseObject obj = new ResponseObject();
		try{
			int count = qsjkDao.queryTQsjkrwHave(q);
			obj.setData(count);
			obj.setStatus(200);
		}catch(Exception e){
			log.error("查询监控任务是否存在出错", e);
			throw new MyException("查询监控任务是否存在出错",e);
		}
		return obj;
	}

	@Transactional("localTransactionManager")
	@Override
	public ResponseObject qsjkZx(TQsjkrwVo q) {
		ResponseObject obj = new ResponseObject();
		try{
			List<TQsjkrwVo> list = qsjkDao.queryTQsjkrw(q);
			if(CommonUtil.isNotEmptyList(list))
			{
				List<TQsjkrwTzVo> tzlist = new ArrayList<>();
				
				StringBuilder message = new StringBuilder();
				for(int i = 0;i < list.size();i ++)
				{
					int result = 0;
					if("GC01".equals(list.get(i).getRwbh()))
					{
						result = qsjkZxService.gc01(list.get(i));
					}
					if("GC02".equals(list.get(i).getRwbh()))
					{
						result = qsjkZxService.gc02(list.get(i));
					}
					if("GC03".equals(list.get(i).getRwbh()))
					{
						result = qsjkZxService.gc03(list.get(i));
					}
					if("GC05".equals(list.get(i).getRwbh()))
					{
						result = qsjkZxService.gc05(list.get(i));
					}
					if("GS02".equals(list.get(i).getRwbh()))
					{
						result = qsjkZxService.Gs02(list.get(i));
					}
					if("GS03".equals(list.get(i).getRwbh()))
					{
						result = qsjkZxService.Gs03(list.get(i));
					}
					if("GC04".equals(list.get(i).getRwbh()))
					{
						result = qsjkZxService.gc04(list.get(i));
					}
					if("GC07".equals(list.get(i).getRwbh()))
					{
						result = qsjkZxService.gc07(list.get(i));
					}
					if("GCW01".equals(list.get(i).getRwbh()))
					{
						result = qsjkZxService.gcw01(list.get(i));
					}
					if("GCW02".equals(list.get(i).getRwbh()))
					{
						result = qsjkZxService.gcw02(list.get(i));
					}
					if("GCXY01".equals(list.get(i).getRwbh()))
					{
						result = qsjkZxService.gcXY01(list.get(i));
					}
					if("GS04".equals(list.get(i).getRwbh()))
					{
						result = qsjkZxService.gs04(list.get(i));
					}
					if("GS05".equals(list.get(i).getRwbh()))
					{
						result = qsjkZxService.gs05(list.get(i));
					}
					if("GS06".equals(list.get(i).getRwbh()))
					{
						result = qsjkZxService.gs06(list.get(i));
					}
					if("GS07".equals(list.get(i).getRwbh()))
					{
						result = qsjkZxService.gs07(list.get(i));
					}
					if("GS08".equals(list.get(i).getRwbh()))
					{
						result = qsjkZxService.gs08(list.get(i));
					}
					if("GS09".equals(list.get(i).getRwbh()))
					{
						result = qsjkZxService.gs09(list.get(i));
					}
					if("GS10".equals(list.get(i).getRwbh()))
					{
						result = qsjkZxService.gs10(list.get(i));
					}
					if("GS11".equals(list.get(i).getRwbh()))
					{
						result = qsjkZxService.gs11(list.get(i));
					}
					if("GS12".equals(list.get(i).getRwbh()))
					{
						result = qsjkZxService.gs12(list.get(i));
					}
					if("GS13".equals(list.get(i).getRwbh()))
					{
						result = qsjkZxService.gs13(list.get(i));
					}
					if("GS14".equals(list.get(i).getRwbh()))
					{
						result = qsjkZxService.gs14(list.get(i));
					}
					if(result == 2)
					{
						if(CommonUtil.isNotEmptyBuilder(message))
						{
							message.append("、");
						}
						message.append(list.get(i).getRwbh());
						if(!"1".equals(list.get(i).getTzfs()))
						{
							TQsjkrwTzVo t = new TQsjkrwTzVo();
							t.setTzfs(list.get(i).getTzfs());
							t.setTzry_gh(list.get(i).getTzry_gh());
							t.setTzry_sj(list.get(i).getTzry_sj());
							t.setContent("【智慧运营服务平台清算监控】<br/>" + list.get(i).getRwmc() + "(" + list.get(i).getRwbh() + ")" + "<font color=#e7453a>执行失败</font>");
							t.setClzt(1);
							tzlist.add(t);
						}
					}
				}
				if(CommonUtil.isNotEmptyList(tzlist))
				{
					List<TQsjkrwTzVo> newTzlist = new ArrayList<>();
					String []arrbadge = new String[tzlist.size()];
					int num = 0;
					for(int i = 0;i <tzlist.size();i ++)
					{
						TQsjkrwTzVo t = tzlist.get(i);
						StringBuilder content = new StringBuilder();
						for(int j = 0;j <tzlist.size();j ++)
						{
							if(!Arrays.asList(arrbadge).contains(tzlist.get(j).getTzry_gh() + "_" + tzlist.get(j).getTzfs()) 
									&& tzlist.get(i).getTzry_gh().equals(tzlist.get(j).getTzry_gh())
									&& tzlist.get(i).getTzfs().equals(tzlist.get(j).getTzfs()))
							{
								
								arrbadge[num] = tzlist.get(i).getTzry_gh() + "_" + tzlist.get(j).getTzfs();
								num ++;
								if(CommonUtil.isNotEmptyBuilder(content))
								{
									content.append("、");
								}
								content.append(tzlist.get(j).getContent());
							}
						}
						t.setTzrq(CommonUtil.getDate());
						t.setContent(content.toString());
						newTzlist.add(t);
					}
					qsjkDao.insertTQsjkrwTz(newTzlist);
				}
				obj.setStatus(200);
				if(CommonUtil.isNotEmptyBuilder(message))
				{
					obj.setMessage("任务执行异常：【" + message + "】");
				}else{
					obj.setMessage("任务执行成功");
				}
			}else{
				obj.setMessage("无执行任务，请确认");
				obj.setStatus(201);
			}
		}catch(Exception e){
			log.error("执行清算监控任务出错", e);
			throw new MyException("执行清算监控任务出错",e);
		}
		return obj;
	}

	@Transactional("localTransactionManager")
	@Override
	public ResponseObject qsjkRg(TQsjkrwVo q) {
		ResponseObject obj = new ResponseObject();
		try{
			q.setLast_zxhs("0");
			q.setLast_zxjg("3");
			q.setLast_jgsm("人工干预置为成功！");
			q.setLast_zxsj(CommonUtil.getDateTime());
			qsjkDao.insertTQsjkrwLs(q);
			qsjkDao.updateTQsjkrwLastLs(q);
			obj.setStatus(200);
		}catch(Exception e){
			log.error("人工干预清算监控任务出错", e);
			throw new MyException("人工干预清算监控任务出错",e);
		}
		return obj;
	}
	
}
